package com.example.forecastweather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/*
 * class nay phan tich ban tin JSON ve thoi tiet lay ve
 */
public class JSONWeatherParse {
	public static Weather getWeather(String data) throws JSONException {
		Weather weather = new Weather();
		System.out.println("Data [" + data + "]");
		// khoi tao doi tuong jsonObject tu data
		JSONObject jObj = new JSONObject();
		Location loc = new Location();

		// JSONArray arrayListTemp = jObj.getJSONArray("list");

		// lay thong tin dia diem du bao thoi tiet

		JSONObject city = jObj.getJSONObject("city");
		loc.setCity(city.getString("name"));
		loc.setCountry(city.getString("country"));
		JSONObject location = city.getJSONObject("coord");
		loc.setLat(location.getDouble("lat"));
		loc.setLon(location.getDouble("lon"));

		// JSONObject coorobj = getObject("coord", jObj);

		// loc.setLat(getFloat("lat", coorobj));
		// loc.setLon(getFloat("lon", coorobj));
		// System.out.println(loc.setLat(getFloat("lat", coorobj)));

		// JSONObject jCity = getObject("city", jObj);
		// loc.setCity(getString("name", jCity));

		// JSONObject jCountry = getObject("country", jObj);
		// loc.setCountry(getString("country", jCountry));

		weather.location = loc;

		JSONArray jArray = jObj.getJSONArray("list");
		JSONObject jObject = jArray.getJSONObject(0);

		JSONObject jTemp = jObject.getJSONObject("temp");
		weather.temp.setTemp(jTemp.getDouble("day"));
		weather.temp.setMax(jTemp.getDouble("max"));
		weather.temp.setMin(jTemp.getDouble("min"));

		JSONObject jhumidity = jObject.getJSONObject("humidity");
		weather.current.setHumidity(jhumidity.getDouble("humidity"));

		JSONObject jPressure = jObject.getJSONObject("pressure");
		weather.current.setPressure(jPressure.getDouble("pressure"));

		// khoi tao mang lay thong tin ra
		JSONArray jarray = jObject.getJSONArray("weather");

		// lay gia tri dau tien tuc thong tin thoi tiet hien tai
		JSONObject JSONObj = jarray.getJSONObject(0);
		weather.current.setId(JSONObj.getInt("id"));
		weather.current.setDescr(JSONObj.getString("description"));
		weather.current.setMain(JSONObj.getString("main"));
		weather.current.setIcon(JSONObj.getString("icon"));

		// wind

		weather.wind.setSpeed(jObject.getDouble("speed"));
		// weather.wind.setDeg(jWind.getDouble("deg"));

		// rain

		weather.wind.setDeg(jObject.getDouble("deg"));

		// cloud

		weather.clouds.setPerc(jObject.getDouble("clouds"));

		return weather;

	}

	/*
	 * public static JSONObject getObject(String tagname, JSONObject jobj)
	 * throws JSONException { JSONObject subjobj = jobj.getJSONObject(tagname);
	 * return jobj; }
	 * 
	 * public static String getString(String tagname, JSONObject jobj) throws
	 * JSONException { return jobj.getString(tagname); }
	 * 
	 * public static Float getFloat(String tagString, JSONObject jobj) throws
	 * JSONException { return (float) jobj.getDouble(tagString); }
	 * 
	 * public static int getInt(String tagname, JSONObject jobj) throws
	 * JSONException { return jobj.getInt(tagname); }
	 */
}
